package com.github.freewu.mvp.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.freewu.mvp.bean.Mvp;
import com.github.freewu.mvp.mvpInterface.MvpInterface;
import com.github.freewu.mvp.mvpInterface.ViewInit;
import com.github.freewu.mvp.presenter.Presenter;
import com.github.freewu.mvp.view.status.SimpleStatusView;
import com.github.freewu.mvp.view.status.StatusController;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by 杨哲 on 2016/4/21 0021.
 */
public abstract class BaseFragment extends Fragment implements ViewInit,MvpInterface.ViewInterface {
    protected View mView;

    protected Context mContext;

    protected List<Presenter> presenters=new LinkedList<>();

    protected StatusView statusView;

    private boolean isCreate =false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView==null) {
            mContext = new ContextThemeWrapper(getActivity().getApplicationContext(), Mvp.config.getmStyle());

            mView = inflater.inflate(getLayout(), null);

            if (Mvp.config.getStatusView()!=null) {
                statusView = Mvp.config.getStatusView();
            }else{
                statusView =new SimpleStatusView(getContext());
            }
            statusView.setNormalView(mView);

            initView(mView);

            setStatusViewListener();

            initData();
        }
        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onCreateView();
            }
        }
        return statusView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onDestroy();
            }
        }
        if (statusView!=null){
            statusView.onDestroy();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onCreate();
            }
        }
        if (statusView!=null){
            statusView.onCreate();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (!isCreate){
            return;
        }

        if (isVisibleToUser){
            canVisible();
        }else{
            noneVisible();
        }
    }

    /**
     * 懒加载，获取到了焦点
     */
    protected void canVisible(){

    }
    /**
     * 懒加载，失去了焦点
     */
    protected void noneVisible(){

    }

    @Override
    public void onPause() {
        super.onPause();

        if (presenters !=null) {
            for(Presenter presenter:presenters){
                presenter.onPause();
            }
        }
    }

    @Override
    public Intent getIntent() {
        return getActivity().getIntent();
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public StatusView getStatusView() {
        return statusView;
    }

    @Override
    public StatusController getStatusController(){
        return getStatusView();
    }

    @Override
    public void addPresenter(Presenter presenter) {
        this.presenters.add(presenter);
    }

    @Override
    public void OnReloadViewClick(View v) {

    }

    @Override
    public void OnErrorViewClick(View v) {

    }

    protected void setStatusViewListener(){
        getStatusView().getReloadView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnReloadViewClick(v);
            }
        });
        getStatusView().getErrorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnErrorViewClick(v);
            }
        });
    }

}
